package com.study.demo.service;

import com.study.demo.dto.User;
import com.study.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

@Service
public class JpaService {

    @Autowired
    private UserRepository userRepository;

    public User findByNameAndAge(String name, Integer age) {
        return userRepository.findByNameAndAge(name, age);
    }

    public User insert(User user) {
       return userRepository.save(user);
    }

    public List<User> findByAge(Integer age, int pageSize, int pageNum) {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        return userRepository.findByAge(age, pageRequest);
    }

    public User findFirstByOrderByAgeAsc() {
        return userRepository.findFirstByOrderByAgeDesc();
    }

    public User findByNameAndSex(String name, Integer sex) {
        return userRepository.findByNameAndSex(name, sex);
    }
}
